<!-- 订单管理 -->
<template>
	<section>
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;text-align: right;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-dropdown size="small" placement="bottom" trigger="click" @command="batchView" style="margin-left: 10px;">
					    <el-button type="primary" class="search-btn">
							查询订单
							<i class="el-icon-arrow-down el-icon--right"></i>
					    </el-button>
					    <el-dropdown-menu slot="dropdown">
						    <el-dropdown-item command="ship_View" style='width: 100px;margin-bottom: 5px;text-align: center;'>
							   <el-button type="primary" size="small" icon="el-icon-view">待发货</el-button>
							</el-dropdown-item>
							<el-dropdown-item command="rece_View" style='width: 100px;margin-bottom: 5px;text-align: center;'>
								<el-button type="primary" size="small" icon="el-icon-view">待收货</el-button>
							</el-dropdown-item>
							<el-dropdown-item command="carr_View" style='width: 100px;margin-bottom: 5px;text-align: center;'>
								<el-button type="primary" size="small" icon="el-icon-view">已完成</el-button>
							</el-dropdown-item>
							<el-dropdown-item command="cus_View" style='text-align: center;'>
								<el-button type="primary" size="small" icon="el-icon-view">自定义</el-button>
							</el-dropdown-item>
					    </el-dropdown-menu>
					</el-dropdown>
				</el-form-item>
				<el-form-item>
					<el-dropdown size="small" placement="bottom" trigger="click" @command="batchOperate" style="margin-left: 10px;">
					    <el-button type="success" class="search-btn">
							导出订单
							<i class="el-icon-arrow-down el-icon--right"></i>
					    </el-button>
					    <el-dropdown-menu slot="dropdown">
						    <el-dropdown-item command="ship_excel" style='width: 100px;margin-bottom: 5px;text-align: center;'>
							   <el-button type="primary" size="small" icon="el-icon-download">待发货</el-button>
							</el-dropdown-item>
							<el-dropdown-item command="rece_excel" style='width: 100px;margin-bottom: 5px;text-align: center;'>
								<el-button type="primary" size="small" icon="el-icon-download">待收货</el-button>
							</el-dropdown-item>
							<el-dropdown-item command="carr_excel" style='width: 100px;margin-bottom: 5px;text-align: center;'>
								<el-button type="primary" size="small" icon="el-icon-download">已完成</el-button>
							</el-dropdown-item>
							<el-dropdown-item command="cus_excel" style='text-align: center;'>
								<el-button type="primary" size="small" icon="el-icon-download">自定义</el-button>
							</el-dropdown-item>
					    </el-dropdown-menu>
					</el-dropdown>
				</el-form-item>
				<el-form-item >
					<el-button type="primary" @click="dialogVisible = true" icon="el-icon-upload">导入快递</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="List" highlight-current-row v-loading="loading" @selection-change="selsChange" border :cell-style="cellStyle" :header-cell-style="rowClass" style="width: 100%;">
			<el-table-column prop="id" align="center" label="ID" width="60"></el-table-column>
			<el-table-column prop="realName" align="center" label="用户名" width="130"></el-table-column>
			<el-table-column prop="phone" align="center" label="手机号" width="110"></el-table-column>
			<el-table-column prop="orderNumber" align="center" label="订单号" width="200"></el-table-column>
			<el-table-column prop="goodsName" align="center" label="商品名称" width="250"></el-table-column>
			<el-table-column prop="number" align="center" label="数量" ></el-table-column>
			<el-table-column prop="payAmount" align="center" label="支付价格" ></el-table-column>
			<el-table-column prop="realPayAmount" align="center" label="实付价格" ></el-table-column>
			<el-table-column prop="state" align="center" label="订单状态" :formatter="formatSex"></el-table-column>
			<el-table-column prop="expressState" align="center" label="物流状态">
				<template slot-scope="scope">
					<p v-if="scope.row.expressState == '0'">待发货</p>
					<p v-else-if="scope.row.expressState == '1'">待收货</p>
					<p v-else-if="scope.row.expressState == '2'">已完成</p>
				</template>
			</el-table-column>
			<el-table-column prop="expressNumber" align="center" label="物流单号" width="170">
				<template slot-scope="scope">
					<el-button v-if="!scope.row.expressNumber" size="small" type="primary" icon="el-icon-circle-plus-outline" @click="handleEdit(scope.$index, scope.row)">添加物流单号</el-button>
					<p v-else >{{scope.row.expressNumber}}</p>
				</template>
			</el-table-column>
			<el-table-column prop="startTime" align="center" label="创建时间" width="170"></el-table-column>
			<!-- <el-table-column prop="endTime" align="center" label="结束时间" width="170"></el-table-column> -->
			<el-table-column prop="recvName" align="center" label="收货人姓名" width="130"></el-table-column>
			<el-table-column prop="moblie" align="center" label="收货人号码" width="110"></el-table-column>
			<el-table-column prop="address" align="center" label="收货人地址" width="350"></el-table-column>
		</el-table>
		<!--批量删除-->
		<el-col :span="24" class="toolbar">
			<el-pagination background layout="total,sizes,prev, pager, next,jumper" @size-change="handleSizeChange" @current-change="handleCurrentChange" :page-size="10" :total="total" style="float:right;">
			</el-pagination>
		</el-col>
		<!-- 填写物流单号 -->
		<el-dialog title="修改物流信息" :visible.sync="adddialog" width="35%" :before-close="handleClose">
			<el-form :model="editForm" label-width="105px" :rules="editFormRules" ref="editForm">
				<el-form-item label="物流公司" prop="expressCode">
					<el-select v-model="editForm.expressCode" placeholder="请选择物流公司">
						<el-option v-for="item in typeList" :key="item.expressName" :label="item.expressName" :value="item.expressCode"></el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="物流单号" prop="expressNumber">
					<el-input v-model="editForm.expressNumber" auto-complete="off" placeholder="请输入正确的物流单号" style="width: 220px;"></el-input>
				</el-form-item>
			</el-form>
			<span slot="footer" class="dialog-footer">
				<el-button @click="adddialog = false">取 消</el-button>
				<el-button type="primary"  @click.native="addSubmit" >确定</el-button>
			</span>
		</el-dialog>
		<!-- 快递单号导入 -->
		<el-dialog title="批量导入" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
			<el-form :model="addForm" label-width="105px" ref="addForm">
				<el-form-item label="单号导入" prop="import">
					<input type="file" @change="upData($event)" ref="InputFile" name="files" style="position: absolute;height: 40px;opacity: 0;"/>
					<el-button type="primary" >选择文件</el-button>
					<div class="el-upload__tip">单号选择后自动上传</div>
				</el-form-item>
			</el-form>
		</el-dialog>
		<!-- 自定义查询 cusexcel-->
		<el-dialog title="自定义查询订单" :visible.sync="cusdialog" width="35%" :before-close="handleClose">
			<el-form :model="cusForm" label-width="105px" ref="cusForm">
				<el-form-item label="订单单号" prop="orderNumber">
					<el-input v-model="cusForm.orderNumber" auto-complete="off" placeholder="请输入正确的订单单号" style="width: 220px;"></el-input>
				</el-form-item>
				<el-form-item label="物流单号" prop="expressNumber">
					<el-input v-model="cusForm.expressNumber" auto-complete="off" placeholder="请输入正确的物流单号" style="width: 220px;"></el-input>
				</el-form-item>
				<el-form-item label="下单日期" prop="orderDate">
					<el-date-picker v-model="cusForm.orderDate" align="right" type="date" placeholder="选择下单日期" format="yyyy-MM-dd" value-format="yyyy-MM-dd"></el-date-picker>
				</el-form-item>
				<el-form-item label="寄件人手机号" prop="phone">
					<el-input v-model="cusForm.phone" auto-complete="off" placeholder="请输入正确的寄件人手机号" style="width: 220px;"></el-input>
				</el-form-item>
				<el-form-item label="收货人手机号" prop="moblie">
					<el-input v-model="cusForm.moblie" auto-complete="off" placeholder="请输入正确的收货人手机号" style="width: 220px;"></el-input>
				</el-form-item>
				<el-form-item label="查询状态选择" prop="status">
					<el-radio-group v-model="cusForm.status">
						<el-radio class="radio" :label="0">待发货</el-radio>
						<el-radio class="radio" :label="1">待收货</el-radio>
						<el-radio class="radio" :label="2">已完成</el-radio>
					</el-radio-group>
				</el-form-item>
			</el-form>
			<span slot="footer" class="dialog-footer">
				<el-button @click="cusdialog = false">取 消</el-button>
				<el-button type="primary"  @click.native="cusSubmit" >确定</el-button>
			</span>
		</el-dialog>
		<!-- 自定义导出 -->
		<el-dialog title="自定义导出订单" :visible.sync="cusexcel" width="35%" :before-close="handleClose">
			<el-form :model="excelForm" label-width="105px" ref="excelForm">
				<el-form-item label="订单单号" prop="orderNumber">
					<el-input v-model="excelForm.orderNumber" auto-complete="off" placeholder="请输入正确的订单单号" style="width: 220px;"></el-input>
				</el-form-item>
				<el-form-item label="物流单号" prop="expressNumber">
					<el-input v-model="excelForm.expressNumber" auto-complete="off" placeholder="请输入正确的物流单号" style="width: 220px;"></el-input>
				</el-form-item>
				<el-form-item label="下单日期" prop="orderDate">
					<el-date-picker v-model="excelForm.orderDate" align="right" type="date" placeholder="选择下单日期" format="yyyy-MM-dd" value-format="yyyy-MM-dd"></el-date-picker>
				</el-form-item>
				<el-form-item label="寄件人手机号" prop="phone">
					<el-input v-model="excelForm.phone" auto-complete="off" placeholder="请输入正确的寄件人手机号" style="width: 220px;"></el-input>
				</el-form-item>
				<el-form-item label="收货人手机号" prop="moblie">
					<el-input v-model="excelForm.moblie" auto-complete="off" placeholder="请输入正确的收货人手机号" style="width: 220px;"></el-input>
				</el-form-item>
				<el-form-item label="下载状态选择" prop="status">
					<el-radio-group v-model="excelForm.status">
						<el-radio class="radio" :label="0">待发货</el-radio>
						<el-radio class="radio" :label="1">待收货</el-radio>
						<el-radio class="radio" :label="2">已完成</el-radio>
					</el-radio-group>
				</el-form-item>
			</el-form>
			<span slot="footer" class="dialog-footer">
				<el-button @click="cusexcel = false">取 消</el-button>
				<el-button type="primary"  @click.native="excelSubmit" >确定</el-button>
			</span>
		</el-dialog>
	</section>
</template>
<script>
	import axios from 'axios';
	import qs from 'qs';
	import util from '../../../common/js/util'
	import {
		getOrderList,
		downloadList,
		editNumber,
		UploadExpress,
		getexpressList,
		Refreshtoken
	} from '../../../api/api';

	export default {
		data() {
			return {
				radio: '',
				filters: {
					name: ''
				},
				loading: false,
				List: [],
				type:0,
				total: 0,
				page: 1,
				size:10,
				sels: [],//列表选中列
				adddialog:false,
				listLoading: false,
				timer: null,
				dialogVisible:false,
				fileList: [],
				limitNum: 1,
				addForm:{},
				editForm:{
					expressCode:'',//快递单号
					expressNumber:'',//物流单号
				},
				editFormRules: {
					expressCode: [{
						required: true,
						message: '请输入物流公司',
						trigger: 'blur'
					}],
					expressNumber: [{
						required: true,
						message: '请输入物流单号',
						trigger: 'blur'
					}]
				},
				refresh: true,
				percentageNum: 100,
				updateTime: '',
				tableDataLoading: true,
				typeList:[],
				// 自定义查询
				cusdialog:false,
				cusForm:{
					orderNumber:'',//快递单号
					expressNumber:'',//物流单号
					orderDate:'',
					phone:'',
					moblie:'',
					status:'-1'
				},
				//自定义下载
				cusexcel:false,
				excelForm:{
					orderNumber:'',//快递单号
					expressNumber:'',//物流单号
					orderDate:'',
					phone:'',
					moblie:'',
					status:'-1'
				}
			}
		},
		mounted() {
			var Token = sessionStorage.getItem('Token');
			var timestamp=new Date().getTime();
			let para = {
				token: Token,
				curpage:this.page,
				pagesize:this.size,
				timestamp: timestamp
			};
			this.loading = true;
			getOrderList(para).then((res) => {
				var res = res.data;
				if (res.code != 0) {
					this.$message({
						message: res.message,
						type: 'error'
					});
				} else {
					this.total = res.data.total;
					this.List = res.data.list;
					this.loading = false;
				}
			});
			
			this.queryInfo();
			this.timer = setInterval(() => {
			   setTimeout(this.queryInfo, 0)
		    }, 600000)
			
			// 获取快递公司名称
			getexpressList(para).then((res) => {
				var res = res.data;
				if (res.code != 0) {
					this.$message({
						message: res.message,
						type: 'error'
					});
				} else {
					this.typeList = res.data;
					this.loading = false;
				}
			});
			
		},
		methods: {
			// 设置定时器
			queryInfo() {
				var Token = sessionStorage.getItem('Token');
				var timestamp=new Date().getTime();
				let para = {token: Token,timestamp: timestamp};
				Refreshtoken(para).then((res) => {
					var res = res.data;
					if (res.code != 0) {
						this.$message({
							message: res.message,
							type: 'error'
						});
					} else {
						this.loading = false;
					}
				});
			},
			// 转换订单状态
			formatSex: function (row, column) {
				return row.state == 1 ? '已付款' : row.state == 0 ? '未付款' : '未知';
			},
			selsChange: function (sels) {
				this.sels = sels;
			},
			cellStyle({row, column}){
				return 'text-align:center;padding:0;';
			},
			rowClass({row, column}){
				return 'text-align:center';
			},
			handleSizeChange(val) {
				if (this.fies == 0) {
					var a = this.fies
				} else if(this.fies == 1){
					var a = this.fies
				} else if(this.fies == 2){
					var a = this.fies
				}
				this.size = val;
				this.getUsers(a);
			},
			handleCurrentChange(currentPage) {
				if (this.fies == 0) {
					var a = this.fies
				} else if(this.fies == 1){
					var a = this.fies
				} else if(this.fies == 2){
					var a = this.fies
				}
				this.page = currentPage;
				this.getUsers(a);
			},
			// 填写物流单号
			handleEdit(index,row){
				this.adddialog = true;
				this.editForm = Object.assign({}, row);
				this.pid = row.id
			},
			addSubmit: function () {
				console.log(this.editForm.expressCode)
				this.$refs.editForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.adddialog = true;
							var Token = sessionStorage.getItem('Token');
							var timestamp = new Date().getTime();
							let para = {token: Token,id:this.pid,expressNumber:this.editForm.expressNumber,timestamp: timestamp,expressCode:this.editForm.expressCode};
							editNumber(para).then((res) => {
								var res = res.data;
								if (res.code != 0) {
									this.$message({
										message: res.message,
										type: 'error'
									});
								} else {
									this.$message({
										type: 'success',
										message: '物流信息提交成功'
									});
									this.$refs['editForm'].resetFields();
									this.adddialog = false;
									this.loading = false;
									this.getUsers();
								}
							});
						});
					}
				})
			},
			// 导入快递单号
			handleClose(done) {
				this.$confirm('确认关闭？')
					.then(_ => {
						done();
					})
				.catch(_ => {});
			},
			upData(event) {
				// 使用formapi打包
				var Token = sessionStorage.getItem('Token');
				var timestamp=new Date().getTime();
				var formData = new FormData() // 声明一个FormData对象
				var formData = new window.FormData() // vue 中使用 window.FormData(),否则会报 'FormData isn't definded'
				formData.append('excelFile', document.querySelector('input[type=file]').files[0]) 
				formData.append('token', Token);
				formData.append('timestamp',timestamp);
				axios({
				  method:"post",
				  url: "/manager/activityOrder/uploadOrderExpress",
				  headers:{
					'Content-type': 'multipart/form-data'
				  },
				  data:formData
				}).then(res=>{
					var res = res.data;
					if (res.code != 0) {
						this.$message({
							message: res.message,
							type: 'error'
						});
					} else {
						this.$message({
							message: '导入成功',
							type: 'success'
						});
						this.loading = false;
						this.dialogVisible = false;
						this.getUsers();
					}
				},err =>{
				});
			},
			//查询订单
			batchView(command){
				switch (command) {
				  case "ship_View":
					this.shipView(0);
					this.fies = 0;
					break;
				  case "rece_View":
					this.receView(1);
					this.fies = 1;
					break;
				  case "carr_View":
					this.carrView(2);
					this.fies = 2;
					break;
				  case "cus_View":
				  this.cusdialog = true;
				  break;
				  
				}
			},
			shipView(ind) {this.getUsers(ind)},
			receView(ind) {this.getUsers(ind)},
			carrView(ind) {this.getUsers(ind)},
			//获取用户列表 判断用户是否在那个列表中
			getUsers: function(type) {
				var Token = sessionStorage.getItem('Token');
				var timestamp=new Date().getTime();
				let para = {
					token: Token,
					curpage:this.page,
					pagesize:this.size,
					expressState:type,
					timestamp: timestamp
				};
				this.loading = true;
				getOrderList(para).then((res) => {
					var res = res.data;
					if (res.code != 0) {
						this.$message({
							message: res.message,
							type: 'error'
						});
					} else {
						this.total = res.data.total;
						this.List = res.data.list;
						this.loading = false;
					}
				});
			},
			//条件查询
			cusSubmit(){
				console.log(this.cusForm.status)
				if (this.cusForm.status == '-1') {
					this.$notify({ title: '警告', message: '查询状态状态为必选项', type: 'warning' });
				}else{
					this.$refs.cusForm.validate((valid) => {
						if (valid) {
							this.$confirm('确认提交吗？', '提示', {}).then(() => {
								this.cusdialog = true;
								var Token = sessionStorage.getItem('Token');
								var timestamp = new Date().getTime();
								if (this.cusForm.orderNumber != '') {
									var para = {token: Token,timestamp: timestamp,curpage:this.page,pagesize:this.size,expressState:this.cusForm.status,orderNumber:this.cusForm.orderNumber};
								}else if(this.cusForm.expressNumber != ''){
									var para = {token: Token,timestamp: timestamp,curpage:this.page,pagesize:this.size,expressState:this.cusForm.status,expressNumber:this.cusForm.expressNumber};
								}else if(this.cusForm.orderDate != ''){
									var para = {token: Token,timestamp: timestamp,curpage:this.page,pagesize:this.size,expressState:this.cusForm.status,orderDate:this.cusForm.orderDate};
								}else if(this.cusForm.phone != ''){
									var para = {token: Token,timestamp: timestamp,curpage:this.page,pagesize:this.size,expressState:this.cusForm.status,phone:this.cusForm.phone};
								}else if(this.cusForm.moblie != ''){
									var para = {token: Token,timestamp: timestamp,curpage:this.page,pagesize:this.size,expressState:this.cusForm.status,moblie:this.cusForm.moblie};
								}
								getOrderList(para).then((res) => {
									var res = res.data;
									if (res.code != 0) {
										this.$message({
											message: res.message,
											type: 'error'
										});
									} else {
										this.$message({
											message: '提交成功',
											type: 'success'
										});
										this.$refs['cusForm'].resetFields();
										this.cusdialog = false;
										this.total = res.data.total;
										this.List = res.data.list;
										this.loading = false;
									}
								});
							});
						}
					});
				}
			},
			// 导出列表
			batchOperate(command) {
			 switch (command) {
			    case "ship_excel":
				  this.shipexcel(0);
				  break;
			    case "rece_excel":
				  this.receexcel(1);
				  break;
				case "carr_excel":
				  this.carrexcel(2);
				  break;
				case "cus_excel":
				this.cusexcel = true;
				break;
			 }
		    },
			shipexcel(ind) {this.download(ind)},
		    receexcel(ind) {this.download(ind)},
		    carrexcel(ind) {this.download(ind)},
			download(type){
				var Token = sessionStorage.getItem('Token');
				var timestamp=new Date().getTime();
				let para = {token: Token,expressState:type,timestamp: timestamp};
				this.loading = true;
				downloadList(para).then((res) => {
					var res = res.data;
					if (res.code != 0) {
						this.$message({
							message: res.message,
							type: 'error'
						});
					} else {
						window.location.href = res.data
						this.loading = false;
						
					}
				});
			},
			// 自定义导出
			excelSubmit(){
				var Token = sessionStorage.getItem('Token');
				var timestamp=new Date().getTime();
				if (this.excelForm.status == '-1') {
					this.$notify({title: '警告',message: '下载状态为必选项',type: 'warning'});
				} else{
					if (this.excelForm.orderNumber != '') {
						var para = {token: Token,expressState:this.excelForm.status,timestamp: timestamp,orderNumber:this.excelForm.orderNumber};
					}else if(this.excelForm.expressNumber != ''){
						var para = {token: Token,expressState:this.excelForm.status,timestamp: timestamp,expressNumber:this.excelForm.expressNumber};
					}else if(this.excelForm.orderDate != ''){
						var para = {token: Token,expressState:this.excelForm.status,timestamp: timestamp,orderDate:this.excelForm.orderDate};
					}else if(this.excelForm.phone != ''){
						var para = {token: Token,expressState:this.excelForm.status,timestamp: timestamp,phone:this.excelForm.phone};
					}else if(this.excelForm.moblie != ''){
						var para = {token: Token,expressState:this.excelForm.status,timestamp: timestamp,moblie:this.excelForm.moblie};
					}
					this.loading = true;
					downloadList(para).then((res) => {
						var res = res.data;
						if (res.code != 0) {
							this.$message({
								message: res.message,
								type: 'error'
							});
						} else {
							this.$refs['excelForm'].resetFields();
							this.cusexcel = false;
							window.location.href = res.data
							this.loading = false;
						}
					});
				}
				
			}
		},
		beforeDestroy() {
		  clearInterval(this.timer);        
		  this.timer = null;
		}
	};
</script>

<style scoped>
.el-button.is-disabled{
	color: #bfcbd9;
	    cursor: not-allowed;
	    background-image: none;
	    background-color: #eef1f6;
	    border-color: #d1dbe5;
}
.el-pagination {
    white-space: nowrap;
    padding: 2px 5px;
    color: #48576a;
}
</style>
